$(function(){

	var layer;
	var table;
	
    layui.use('layer', function(){
	  	layer = layui.layer;
	}); 
	
	layui.use('table', function(){
		table = layui.table;
		
		/**
		 * 加载用户表格数据
		 */
		var username = $("input[name='username']").val();
		 table.render({
		    elem: '#userPageList',
		    height: 'full-200',
		    loading:true,
		    url: 'user/list', //数据接口
		    where: {
                'username':$.trim(username)
            	},
		    page: true, //开启分页
		    toolbar: '#addUser', //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
		    defaultToolbar: ['filter', 'print', 'exports'],
		    cols: [[ //表头
		      {type: 'checkbox', fixed: 'left'},
		      {field: 'id', title: 'ID',sort: true, fixed: 'left'},
		      {field: 'username', title: '用户名', fixed: 'left'},
		      {field: 'name', title: '姓名', sort: true, fixed: 'left'},
		      {field: 'phone', title: '电话号码', fixed: 'left'},
		      {field: 'email', title: '邮箱', fixed: 'left'},
		      {field: 'createtime', title: '创建时间', sort: true,
		    	  templet: function(d){
		    	    return createTime(d.createtime); 
		    	 }, fixed: 'left'
		      },
		      {field: 'lastlogintime', title: '最后登录时间', sort: true,
		    	  templet: function(d){
		    	      return createTime(d.lastlogintime); 
		    	  }, fixed: 'left'
		      },
		      {fixed: 'right', title:'操作', toolbar: '#barDemo', fixed: 'left'},
		      {fixed: 'enable', title:'禁用',templet: 
		    	  function(d){
			    	  if (d.enable == 2) {
			    	    return '<a lay-event="enble"><input type="checkbox" checked name="switch" lay-text="ON|OFF" lay-skin="switch"></a>'; 
					  }
			    	  return '<a lay-event="enble"><input type="checkbox" name="switch" lay-text="ON|OFF" lay-skin="switch"></a>';
		      	  }
		      }
		    ]]
		  });
		  
		  //监听行工具事件
		  table.on('tool(userPagetest)', function(obj){
		    var data = obj.data;
		    console.log(data);
		    //console.log(obj)
		    if(obj.event === 'del'){
		      layer.confirm('真的删除行么', function(index){
		        obj.del();
		        layer.close(index);
		      });
		    } else if(obj.event === 'edit'){
		      layer.prompt({
		        formType: 2
		        ,value: data.email
		      }, function(value, index){
		        obj.update({
		          email: value
		        });
		        layer.close(index);
		      });
		    }else if(obj.event === 'enble'){
		    	//执行后台方法根据id更新
			    }
		  });
			
		  //头工具栏事件
		  table.on('toolbar(userPagetest)', function(obj){
		    var checkStatus = table.checkStatus(obj.config.id);
		    switch(obj.event){
		      case 'addUser':
		        layer.open({
					  type: 2, 
					  closeBtn: 1, 
					  shadeClose: true,
					  shade: 0.8,
					  title: '添加用户',
					  area: ['600px', '550px'], 
					  content: ['user/adduserPage', 'no']
					});
		      break;
		    };
		  });
	});
	
	  
	/**
	 * 搜索用户
	 */
	$('#sreach').on('click',function(){
        var type = $(this).data('type');
	        active[type] ? active[type].call(this) : '';
	    });
	    
	    var active = {
		    reload_user: function () {
				var username = $("input[name='username']").val();
				table.reload('userPageList', {
					url: 'user/list', //数据接口
			        where: {
			            username: username
			        }
                 });
		    }
		};
})